package com.hfxt.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hfxt.entity.Contact;
import com.hfxt.service.ContactService;
import com.hfxt.utils.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/contact")
public class ContactController {
    @Autowired
    private ContactService contactService;


    @RequestMapping("/list")
    public Result list(String cname, Integer pageIndex, Integer pageSize) {
        //构建查询条件对象
        LambdaQueryWrapper<Contact> queryWrapper = new LambdaQueryWrapper<>();

        //如果cname 不是空 就查询like cname
        queryWrapper.like(StringUtils.isNotBlank(cname), Contact::getCname, cname);

        //构建mybatisPlus 分页构造器
        Page<Contact> contactPage = new Page<>(pageIndex, pageSize);

        //使用page方法查询
        IPage<Contact> page = contactService.page(contactPage, queryWrapper);

        return new Result(true, 200, "查询完成", page);
    }

    @RequestMapping("/save")
    public Result save(@RequestBody Contact contact) {
        contactService.save(contact);
        return new Result(true, 200, "新增完成", contact);
    }

    @RequestMapping("/del")
    public Result del(Long id) {
        boolean b = contactService.removeById(id);
        return new Result(true, 200, "删除完成", null);
    }

}
